using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Biz;
using EntLibEC.Biz.Report;
using EntLibEC.Cmn;

namespace EntLibEC.View.Report
{
	/// <summary>
	/// Summary description for RMARate.
	/// </summary>
	public partial class RMARate : PageBase
	{
		protected EntLibEC.View.Cmn.ProductStatus ucStatus ;
		protected EntLibEC.View.Cmn.ProductType ucProductType ;
		protected EntLibEC.View.Cmn.Category ucCategory ;
		protected EntLibEC.View.Cmn.StockProduct ucProduct ;
		protected EntLibEC.View.Cmn.PM ucPM ;

	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			base.CheckAccess((int)AppEnum.Privilege.RMAReportSearch,true ,null);
			if(!Page.IsPostBack)
			{
			    ucStatus.BindProductStatus(true);
				ucProductType.BindProductType(true);
				ucCategory.BindCategory();
				ucPM.BindPM(true);
			}
		}

		

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnSearch_Click(object sender, System.EventArgs e)
		{
			Hashtable ht = new Hashtable();
			
			if(ucProductType.ProductTypeSysNo != AppConst.IntNull)
				ht.Add("ProductType" , ucProductType.ProductTypeSysNo);

			if(ucStatus.StatusSysNo != AppConst.IntNull)
				ht.Add("ProductStatus" , ucStatus.StatusSysNo);

			if ( ucCategory.C3SysNo != AppConst.IntNull )
				ht.Add("Category", " c3sysno= " + ucCategory.C3SysNo);
			else if ( ucCategory.C2SysNo != AppConst.IntNull )
				ht.Add("Category", " c2sysno= " + ucCategory.C2SysNo);
			else if ( ucCategory.C1SysNo != AppConst.IntNull )
				ht.Add("Category", " c1sysno= " + ucCategory.C1SysNo);

			if(ucProduct.ProductSysNo != AppConst.IntNull)
				ht.Add("ProductSysNo" , ucProduct.ProductSysNo);

			if(txtManufacturer.Text.Trim() != AppConst.StringNull)
				ht.Add("Manufacturer" , txtManufacturer.Text.Trim());

			if(ucPM.PMSysNo != AppConst.IntNull)
				ht.Add("PM" , ucPM.PMSysNo);

			DataSet ds = ReportManager.GetInstance().GetRMARate(ht);
			dgReport.DataSource = convertDs(ds);
			dgReport.DataBind();
			Assert(lblMessage , (ds.Tables[0].Rows.Count).ToString()+ " records!" , 1);
			
		}

		private DataSet convertDs(DataSet ds)
		{
		    if(!Util.HasMoreRow(ds))
				return ds;

			ds.Tables[0].Columns.Add("productstatus");
			ds.Tables[0].Columns.Add("rmarate");

			foreach(DataRow dr in ds.Tables[0].Rows)
			{
			    dr["productstatus"] = AppEnum.GetProductStatus(Util.TrimIntNull(dr["status"]));
                if (Util.TrimIntNull(dr["saleqty"]) == 0)
                {
                    dr["rmarate"] = "0.00%";
                }
                else
                    dr["rmarate"] = ((int)dr["rmaqty"] * 100 / ((int)dr["saleqty"] * 1.0m)).ToString(AppConst.DecimalFormatWithGroup) + "%";
			}

			return ds ;

		}
	}
}
